/*
 * UserController.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the web-geek, Version 1.0 (the "License");
 *
 */
package org.geek.user.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.geek.common.base.BaseController;
import org.geek.common.entity.ColumnModel;
import org.geek.common.entity.Criteria;
import org.geek.common.entity.ExtGridReturn;
import org.geek.common.entity.ExtPager;
import org.geek.common.entity.ExtReturn;
import org.geek.common.util.EncryptUtil;
import org.geek.domain.Organ;
import org.geek.domain.User;
import org.geek.organ.service.IOrganService;
import org.geek.user.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**   
 * <p>用户controller</p>
 * @author ninglong bhd2973@qq.com   
 * @version V1.0   
 */
@Controller
@RequestMapping("/user/")
public class UserController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Resource
	private IUserService userService;
	@Resource
	private IOrganService organService;
	
	@RequestMapping(value="initPage.jhtml",method=RequestMethod.GET)
	@Override
	public String initPage(HttpServletRequest request,ModelMap model) {
		
		List<ColumnModel> list = new ArrayList<ColumnModel>();
		list.add(new ColumnModel("用户Id","id",true,true));
		list.add(new ColumnModel("账号","account",false,false));
		list.add(new ColumnModel("用户名","realName",false,false));
		list.add(new ColumnModel("机构","organName",false,false));
		list.add(new ColumnModel("机构Id","organId",false,false));
		list.add(new ColumnModel("密码","password",true,false));
		list.add(new ColumnModel("性别","sex","renderSex",false,false));
		list.add(new ColumnModel("email","email",false,false));
		list.add(new ColumnModel("电话","mobile",false,false));
		list.add(new ColumnModel("备注","remark",false,false));
		model.put("columnModel", list);
		
		model.put("parentWinId", request.getParameter("parentWinId"));
		model.put("winId", request.getParameter("winId"));
		
		log(request,logger,"访问用户管理");
		return "page/user";
	}
	
	@RequestMapping(value="getDataList.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object getDataList(ExtPager pager,HttpServletRequest request){
		Criteria criteria = new Criteria();
		List<Organ> organs = organService.getOrganByCriteria(criteria);
		String organId = request.getParameter("organId");
		if(StringUtils.isNotEmpty(organId)){
			criteria.put("organId", organId);
		}
		int total = userService.countUserByCriteria(criteria);
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setEnd(pager.getLimit() + pager.getStart());
			criteria.setStart(pager.getStart());
		}
		
		List<User> list = userService.getUserByCriteria(criteria);
		for(User user : list){
			for(Organ organ : organs){
				if(user.getOrganId()==organ.getId()){
					user.setOrganName(organ.getOrganName());
					break;
				}
			}
		}
		return new ExtGridReturn(total, list);
	}
	
	@RequestMapping(value="addUser.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object addUser(User user,HttpServletRequest request){
		boolean success = true;
		
		if(StringUtils.isEmpty(user.getAccount()))success = false;
		if(StringUtils.isEmpty(user.getPassword()))success = false;
		if(StringUtils.isEmpty(user.getRealName()))success = false;
		
		user.setPassword(EncryptUtil.md5Digest(user.getPassword()));//md5密码
		if(success && userService.addUser(user)<=0){
			success = false;
		}
		String msg = success?"用户添加成功！":"用户添加失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
	@RequestMapping(value="modUser.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object modUser(User user,HttpServletRequest request){
		boolean success = true;
		
		if(StringUtils.isEmpty(user.getAccount()))success = false;
		if(StringUtils.isEmpty(user.getPassword()))success = false;
		if(StringUtils.isEmpty(user.getRealName()))success = false;
		
		if(success && userService.modUser(user)<=0){
			success = false;
		}
		String msg = success?"用户修改成功！":"用户修改失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}


	@RequestMapping(value="delUserByUserId.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object delUserByUserId(HttpServletRequest request,int id){
		boolean success = true;
		if(userService.delUserByUserId(id)<=0){
			success = false;
		}
		String msg = success?"用户删除成功！":"用户删除失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
}
